package Chen.rbac1.domain.service;
import Chen.rbac1.domain.dto.DeptDto;
import Chen.rbac1.domain.entity.Dept;
import Chen.rbac1.domain.vo.DeptVo;
import Chen.rbac1.domain.vo.Result;

import java.lang.reflect.Array;
import java.util.Arrays;

public class DeptService {
    public static int DATA_SIZE=100;

    public static Dept[] data=new Dept[DATA_SIZE+1];

    public static int idCounter=1;

    public static DeptVo convertToVo(Dept dept) {
        return new DeptVo(dept.id,dept.name,dept.phone);
    }
    public static Result list(){
        DeptVo[] temp=new DeptVo[DATA_SIZE];
        int length=0;
        for(int i=0;i<idCounter;i++){
            if(data[i]!=null){
                temp[length++]=convertToVo(data[i]);
            }
        }
        return Result.success("查询成功", Arrays.copyOf(temp,length));
    }
    public static Result create(DeptDto dto){
        if(idCounter>DATA_SIZE){
            return Result.faliure("创建失败，超出了存储容量");
        }
        Dept dept=new Dept(dto.name,dto.phone);
        dept.id=idCounter++;
        data[dept.id]=dept;
        return Result.success("创建成功",null);
    }
    public static Result update(DeptDto dto,int id){
        if(id<1||id>idCounter){
            return Result.faliure("更新失败，不存在该数据");
        }
        Dept dept=data[id];
        dept.name=dto.name;
        dept.phone=dto.phone;
        return Result.success("更新成功",null);
    }

    public static Result delete(int id){
        if(id<1||id>idCounter||data[id]==null){
            return Result.faliure("删除失败，不存在该数据");
        }
        data[id]=null;
        return Result .success("删除成功",null);

    }
}
